package com.spic.business.vehicle.carInfo.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.spic.business.vehicle.carInfo.constant.CarConstant;
import com.spic.business.vehicle.carInfo.domain.CarInfoDto;
import com.spic.business.vehicle.carInfo.service.ICarInfoservice;
import com.spic.common.core.exception.ServiceException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author : 赵祥
 * create at:  2022/11/4  17:24
 * @Description :
 **/
@Service
public class XHBLCarInfoServiceImpl implements ICarInfoservice {
    @Override
    public List<CarInfoDto> selectCarList() {
//        List<String> list = new ArrayList<>();
//        list.add("03通勤车");
//        list.add("01生产指挥车");
//        list.add("通勤车");
//        list.add("北矿通勤车");
//        list.add("南矿通勤车");

        String s = HttpUtil.get(CarConstant.XHBL_LOGIN_URL, 5000);
        JSONObject resultJson = JSONObject.parseObject(s);
        if(resultJson.getInteger("result") == 0) {
            String jsession = resultJson.getString("jsession");
            String carUrl = CarConstant.XHBL_CAR_URL + jsession;
            String jsonStr = HttpUtil.post(carUrl, "", 5000);
            //System.out.println(jsonStr);
            JSONObject resultObj = JSONObject.parseObject(jsonStr);
            if (resultObj.getInteger("result") == 0) {
                JSONArray vehicles = resultObj.getJSONArray("vehicles");
                List<CarInfoDto> carList = new ArrayList<>();
                for (int i = 0; i < vehicles.size(); i++) {
                    JSONObject obj = (JSONObject)vehicles.get(i);
                    CarInfoDto carInfoDto = new CarInfoDto();
                    carInfoDto.setLicenseplate(obj.getString("nm"));
                    //carInfoDto.setCarType(obj.getString("pnm"));
                    carList.add(carInfoDto);
                }
                //获取车辆位置
                String vehiIdno = carList.stream().map(CarInfoDto::getLicenseplate).collect(Collectors.joining(","));
                Map<String, Object> paramMap = new HashMap<>();
                paramMap.put("geoaddress", "1");
                paramMap.put("toMap", "2");
                paramMap.put("jsession", jsession);
                paramMap.put("vehiIdno", vehiIdno);
                String result = HttpUtil.post(CarConstant.XHBL_CAR_POSITION, paramMap, 5000);
                JSONObject positionResultObj = JSONObject.parseObject(result);
                if (positionResultObj.getInteger("result") == 0) {
                    JSONArray infos = positionResultObj.getJSONArray("infos");
                    for (int i = 0; i < carList.size(); i++) {
                        String licenseplate = carList.get(i).getLicenseplate();
                        for (int j = 0; j < infos.size(); j++) {
                            if (licenseplate.equals(infos.getJSONObject(j).getString("vi"))) {
                                carList.get(i).setLatitude(infos.getJSONObject(j).getString("mlat"));
                                carList.get(i).setLongitude(infos.getJSONObject(j).getString("mlng"));
                                carList.get(i).setPosition(infos.getJSONObject(j).getString("pos"));
                                break;
                            }
                        }
                    }
                }
                return carList;
            }
        }
        return null;
    }

    @Override
    public JSONObject getUserMarkers() {
        String s = HttpUtil.get(CarConstant.XHBL_LOGIN_URL, 5000);
        JSONObject resultJson = JSONObject.parseObject(s);
        if (resultJson.getInteger("result") == 0) {
            String jsession = resultJson.getString("jsession");
            String carUrl = CarConstant.XHBL_MARKERS + jsession;
            String jsonStr = HttpUtil.post(carUrl, "", 5000);
            JSONObject resultObj = JSONObject.parseObject(jsonStr);
            return resultObj;
        }
        throw new ServiceException("调用星火登录接口失败");
    }

    public static void main(String[] args) {
        XHBLCarInfoServiceImpl xhblCarInfoService = new XHBLCarInfoServiceImpl();
        //List carInfoDtos = xhblCarInfoService.selectCarList();
        //System.out.println(JSON.toJSONString(carInfoDtos));
        JSONObject userMarkers = xhblCarInfoService.getUserMarkers();
        System.out.println(userMarkers.toJSONString());
    }
}
